#!/bin/bash

set -e

rm -rf bin
echo "cc_binary(name='a', srcs=['a.cc'])" > BUILD

cleanup_exit() {
  echo "Test failed" ${2}
  echo "cc_binary(name='a', srcs=['a.cc'])" > BUILD
  exit ${1}
}

echo "Build should succeed"
../../build.py build :a || cleanup_exit 1 "build :a #1 failed"
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph1 - || cleanup_exit 1 "graph #1 diff"

echo "cc_binary(name='a' srcs=['a.cc'])" > BUILD

echo "Build should fail"
../../build.py build :a && cleanup_exit 1 "build :a #2 succeded"
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph2 - || cleanup_exit 1 "graph #2 diff"

echo "cc_binary(name='a', srcs=['a.cc'])" > BUILD

echo "Build should succeed"
../../build.py build :a || cleanup_exit 1 "build :a #3 failed"
../../pretty_graph_dump.py bin/__graph__/newgraph.db | diff reference_graph1 - || cleanup_exit 1 "graph #3 diff"

rm -rf bin

echo 'Test passed'
